package com.example.dexfiletest;

import java.io.IOException;
import java.io.InputStream;

import com.example.hellojni.HelloJni;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class TestOperator implements DexProxy, View.OnClickListener{

	private View mainView;
	private Activity act;
	private Resources resouses;
	private String dexPath;
	Button btn;
	
	private TestBroadcast testBroadcast;
	public TestOperator(Activity act,String dexPath)
	{
		this.act = act;
		this.dexPath = dexPath;
		boolean isPlug = act.getIntent().getBooleanExtra("plug", false);
		
		if(isPlug){
		//	resouses = DexFileTest.createResources(act, dexPath);
		//	XmlResourceParser parser = resouses.getLayout(R.layout.activity_main1);
			
		//	LayoutInflater layoutInflater = act.getLayoutInflater();
		//	mainView = layoutInflater.inflate(parser, null);
		}else{
			
		}
		
		/*
		resouses = act.getResources();
		XmlResourceParser parser = resouses.getLayout(R.layout.activity_main1);
	
		LayoutInflater layoutInflater = act.getLayoutInflater();
		mainView = layoutInflater.inflate(parser, null);
		*/
		mainView =act.getLayoutInflater().inflate(R.layout.activity_main1, null);
		testBroadcast = new TestBroadcast();
		testBroadcast.regist();
	}
	
	public void onStart()
	{
		
	}
	
	public void onStop()
	{
		
	}
	
	public void onResume()
	{
		
	}
	
	public void onPause()
	{
		
	}
	
	public void onDestroy()
	{
		testBroadcast.unregist();
	}

	@Override
	public void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		String  mNativeLibDir =act.getDir("pluginlib", Context.MODE_PRIVATE).getAbsolutePath();
	//	HelloJni helloJni = new HelloJni(mNativeLibDir+"/libhello-jni.so");
		//btn = (Button)mainView.findViewById(R.id.show);

	///	btn.setText(mNativeLibDir+helloJni.getHelloWord());
		//btn.setOnClickListener(this);
		getAssets();
	}

	
	private void getAssets(){
		 String path = "eee.txt";
		 try {
			 Log.e("input","input is11 ");
			InputStream input = act.getAssets().open(path);
			Log.e("input","input is "+input);
			
			InputStream input1 = act.getAssets().open("eee1.txt");
			Log.e("input","input1 is "+input1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void onRemuse() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onBackPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public View getMainView() {
		// TODO Auto-generated method stubaz
		return mainView;
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		btn.setText("click btn");
		Intent intent = new Intent(TestBroadcast.Test);
		act.sendBroadcast(intent);
	}
	
	private class TestBroadcast extends BroadcastReceiver{

		public static final String Test = "com.fzh.aa";
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			DexFileTest.showText(act, Test);
		}
		
		public void regist()
		{
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction(Test);
			
			act.registerReceiver(this, intentFilter);
		}
		
		public void unregist()
		{
			act.unregisterReceiver(this);
		}
		
	}
}
